package com.ruoyi.eam.service.impl;

import java.util.ArrayList;
import java.util.List;
import com.ruoyi.common.core.utils.DateUtils;
import com.ruoyi.eam.constants.EamConstants;
import com.ruoyi.eam.doman.*;
import com.ruoyi.eam.mapper.*;
import com.ruoyi.eam.service.IEamAllotDService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.eam.service.IEamAllotHService;

/**
 * 资产调拨Service业务层处理
 *
 * @author xl
 * @date 2024-07-12
 */
@Service
public class EamAllotHServiceImpl implements IEamAllotHService
{
    @Autowired
    private EamAllotHMapper eamAllotHMapper;
    @Autowired
    private DeptMapper deptMapper;
    @Autowired
    private EamLocationMapper locationMapper;
    @Autowired
    private UserMapper userMapper;
    @Autowired
    private EamAllotDMapper allotDMapper;
    @Autowired
    private EamAssetMapper assetMapper;

    /**
     * 查询资产调拨
     *
     * @param allotId 资产调拨主键
     * @return 资产调拨
     */
    @Override
    public EamAllotH selectEamAllotHByAllotId(Long allotId)
    {
        return eamAllotHMapper.selectEamAllotHByAllotId(allotId);
    }

    /**
     * 查询资产调拨列表
     *
     * @param eamAllotH 资产调拨
     * @return 资产调拨
     */
    @Override
    public List<EamAllotH> selectEamAllotHList(EamAllotH eamAllotH)
    {
        List<EamAllotH> list = eamAllotHMapper.selectEamAllotHList(eamAllotH);
        for (EamAllotH allotH : list) {
            //调拨部门名称
            allotH.setExportDeptName(deptMapper.selectDeptByDeptId(allotH.getExportDept()).getDeptName());
            //调入部门名称
            allotH.setFoldDeptName(deptMapper.selectDeptByDeptId(allotH.getFoldDept()).getDeptName());
            //存放位置名称
            allotH.setLocationName(locationMapper.selectEamLocationByLocationId(allotH.getLocationId()).getLocationName());
            //制单人
            allotH.setPreparedName(userMapper.selectUserByUserId(allotH.getPrepared()).getNickName());
            //审核人
            if(allotH.getAuditPerson() != null){
                allotH.setAuditPersonName(userMapper.selectUserByUserId(allotH.getAuditPerson()).getNickName());
            }
            EamAllotD allotD = new EamAllotD();
            allotD.setAllotId(allotH.getAllotId());
            List<EamAllotD> allotDS = allotDMapper.selectEamAllotDList(allotD);
            List<EamAsset> assets = new ArrayList<>();
            for (EamAllotD allot : allotDS) {
                EamAsset asset = assetMapper.selectEamAssetByAssetId(allot.getAssetId());
                assets.add(asset);
            }
            allotH.setAssetlist(assets);
        }
        return list;
    }

    /**
     * 新增资产调拨
     *
     * @param eamAllotH 资产调拨
     * @return 结果
     */
    @Override
    public int insertEamAllotH(EamAllotH eamAllotH)
    {
        //业务处理
        eamAllotH.setCreateTime(DateUtils.getNowDate());
        eamAllotH.setPreparedDate(DateUtils.getNowDate());
        //借用编码规则
        //获取当前时间
        String nowDate = DateUtils.getDateYYYYMMDD();
        //获取最后一条借用编码
        String chgCodeLast = eamAllotHMapper.queryEamAllotHList().get(eamAllotHMapper.queryEamAllotHList().size() - 1).getAllotOdd();
        //截取流水线
        String serial =  chgCodeLast.substring(10,16);
        if(serial.equals("999999")){
            serial = "000001";
        }else{
            int num = Integer.parseInt(serial) + 1;
            serial = String.format("%06d",num);
        }
        //最终编码
        String chgCode = EamConstants.DB_CODE_PR + nowDate + serial;
        eamAllotH.setAllotOdd(chgCode);
        eamAllotH.setIsdel(1L);
        eamAllotH.setReceiptsStatus(3L);
        return eamAllotHMapper.insertEamAllotH(eamAllotH);
    }

    /**
     * 修改资产调拨
     *
     * @param eamAllotH 资产调拨
     * @return 结果
     */
    @Override
    public int updateEamAllotH(EamAllotH eamAllotH)
    {
        return eamAllotHMapper.updateEamAllotH(eamAllotH);
    }

    /**
     * 批量删除资产调拨
     *
     * @param allotIds 需要删除的资产调拨主键
     * @return 结果
     */
    @Override
    public int deleteEamAllotHByAllotIds(Long[] allotIds)
    {
        return eamAllotHMapper.deleteEamAllotHByAllotIds(allotIds);
    }

    /**
     * 删除资产调拨信息
     *
     * @param allotId 资产调拨主键
     * @return 结果
     */
    @Override
    public int deleteEamAllotHByAllotId(Long allotId)
    {
        return eamAllotHMapper.deleteEamAllotHByAllotId(allotId);
    }
}
